package com.woniuxy.controller;

import com.woniuxy.pojo.Basic;
import com.woniuxy.service.BasicService;
import com.woniuxy.service.MyLog;
import com.woniuxy.util.DataGridView;
import com.woniuxy.util.ResultDto;
import com.woniuxy.vo.BasicVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class BasicDataController {
    private Logger logger= LoggerFactory.getLogger(this.getClass());
    @Autowired
    private BasicService basicService;
    //基础数据列表
    @GetMapping("/basicDatas")
    public DataGridView listDataAll(BasicVo basicVo){
    return basicService.selectCondition(basicVo);
    }
    @MyLog(value = "编辑基础")  //这里添加了AOP的自定义注解
    //编辑基础数据
    @PostMapping("/basicDataUpdate.do")
    public ResultDto basicDataUpdate(Basic basic){
        logger.debug("是否可编辑"+basic.getBasiEditable());
        logger.debug("类别"+basic.getBasiType());
        logger.debug("条目"+basic.getBasiItem());
        logger.debug("值"+basic.getBasiValue());
        try {
            basicService.updateBasicByCondition(basic);
            return ResultDto.UPDATE_BASICDATA_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return  ResultDto.UPDATE_BASICDATA_FAILURE;
        }
    }
    @MyLog(value = "增加基础数据")  //这里添加了AOP的自定义注解
    //增加基础数据
    @PostMapping(value = "/basicDataAdd.do")
    public ResultDto basicDataAdd(Basic basic){
        Basic basic1=basicService.selectByNo(basic);
        if(basic1!=null){
            return ResultDto.INSERT_BASICDATA_EXISTS;
        }else {
            int i=basicService.insertBasicData(basic);
            if(i==1){
                return ResultDto.INSERT_BASICDATA_SUCCESS;
            }else{
                return ResultDto.INSERT_BASICDATA_FAILURE;
            }
        }
    }
    @MyLog(value = "删除基础数据")  //这里添加了AOP的自定义注解
    //删除数据
    @DeleteMapping(value = "/basicDatas/{basiNo}")
    public ResultDto deleteBasicData(@PathVariable("basiNo")int basiNo){
        try {
            basicService.deleteByNo(basiNo);
            return ResultDto.DELETE_BASICDATA_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.DELETE_BASICDATA_FAILURE;
        }
    }
    @MyLog(value = "批量删除基础数据")  //这里添加了AOP的自定义注解
    //批量删除
    @GetMapping("/basicDatas/deleteBatch")
    public ResultDto deleteBatches(@RequestParam("basiNos[]") int[] basiNos){
        try {
            basicService.deleteBatches(basiNos);
            return ResultDto.DELETE_BASICDATA_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.DELETE_BASICDATA_FAILURE;
        }
    }
}
